ArcPad Scripting Object Model
OpenLayer Example
Send Feedback
ArcPad Scripting Object Model Reference > ArcPad Scripting Samples > OpenLayer Example

Glossary Item Box

Description

Demonstrates how to use the DataSource's OpenLayer method and work with the returned array.

VBScript Code

OpenLayer Copy Code
Function OpenAXF(p_strAXFPath)
      Dim pDS
      Set pDS = Application.CreateAppObject("DataSource")
      pDS.Open(p_strAXFPath)
      If (pDS.IsOpen) Then
            Set OpenAXF = pDS
      Else
            Set OpenAXF = Nothing
      End If
End Function
Function GetFileNameUI(p_extension, p_filter, p_title, p_flags) 
      GetFileNameUI = CommonDialog.ShowOpen(p_extension, p_filter, p_title, p_flags)
      If (IsEmpty(GetFileNameUI)) Then
            GetFileNameUI = ""
      End If
End Function
Sub OpenLayer(p_strLayerName)
      Dim strFileName
      strFileName = GetFileNameUI("axf", "ArcPad AXF Files|*.axf","Select AXF File", &H1000)
      If ("" = strFileName) Then Exit Sub
      Dim pDS
      Set pDS = OpenAXF(strFileName)
      If (pDS Is Nothing) Then
            Console.Print "Open DataSource failed"
            Exit Sub
      End If
      '++ open the passed in layer name
      '++ (note there is no error handling or validation in this sample!!)
      Dim pRS
      'Set pRS = pDS.OpenLayer(p_strLayerName,Map.Extent,"[LANDUSE_ID] < 2000")
      Set pRS = pDS.OpenLayer(p_strLayerName)

      If (Not pRS Is Nothing) Then
            '++ one way of working with the returned RecordSet - 
            '++ step through the records and output the first attribute to the Console window
            Console.Print "Total count: " & pRS.RecordCount
            Console.Print "Number of fields: " & pRS.Fields.Count
            pRS.MoveFirst
            Do While Not pRS.EOF
                  Console.Print pRS.Fields(1).Name & ": " & pRS.Fields(1).Value
                  pRS.MoveNext
            Loop
            
            Console.Print "----------"
            '++ another way of working with the returned RecordSet -
            '++ convert it to a 2 dimensional array and output the attributes
            '++ row by row to the Console window
            Dim arrRecords, cols, rows
            arrRecords = pRS.ToArray
            For rows = 0 to UBound(arrRecords,1)
                  For cols = 0 to UBound(arrRecords,2)
                        Console.Print rows & "," & cols & ": " & arrRecords(rows,cols)
                  Next
            Next
            '++ close the RecordSet
            pRS.Close
            Set pRS = Nothing
      End If
      '++ close the DataSource
        pDS.Close
      Set pDS = Nothing
End Sub
'++ call OpenLayer with the layer Parks
'++ note there is a Parks layer in the sample Riverside.axf installed with ArcPad
Console.Clear
Call OpenLayer("Parks")

JScript Code

 

OpenLayer Copy Code
function OpenAXF(p_strAXFPath)
{
      var pDS = Application.CreateAppObject("DataSource");
      pDS.Open(p_strAXFPath)
      if (pDS.IsOpen)
            return pDS
      else
            return null;
}
function GetFileNameUI(p_extension, p_filter, p_title, p_flags)
{
      var resOpen = CommonDialog.ShowOpen(p_extension, p_filter, p_title, p_flags);
      if (resOpen == null)
            return "";
      else
            return resOpen;
}
function OpenLayer(p_strLayerName)
{
      // prompt for the AXF file
      var strFileName = GetFileNameUI("axf", "ArcPad AXF Files|*.axf","Select AXF File", 0x1000);
      if ("" == strFileName)
            return;
      // open the selected AXF file
      var pDS = OpenAXF(strFileName);
      if (null == pDS)
      {
            Console.Print("Open DataSource failed");
            return;
      }
      // open the passed in layer name
      // (note there is no error handling or validation in this sample!!)
      
      //Set pRS = pDS.OpenLayer(p_strLayerName,Map.Extent,"[LANDUSE_ID] < 2000")
      var pRS = pDS.OpenLayer(p_strLayerName);
      if (pRS != null)
      {
            // one way of working with the returned RecordSet - 
            //step through the records and output the first attribute to the Console window
            Console.Print("Total count: " + pRS.RecordCount);
            Console.Print("Number of fields: " + pRS.Fields.Count);
            pRS.MoveFirst();
            while (!pRS.EOF)
            {
                  Console.Print(pRS.Fields(1).Name + ": " + pRS.Fields(1).Value);
                  pRS.MoveNext();
            }
            Console.Print("----------");
            // another way of working with the returned RecordSet -
            // convert it to a 2 dimensional array and output the attributes
            // row by row to the Console window
            var arrRecords = pRS.ToArray();
            var cols, rows;
            for (rows=arrRecords.lbound(1);rows<=arrRecords.ubound(1);rows++)
                  for(cols=arrRecords.lbound(2);cols<=arrRecords.ubound(2);cols++)
                        Console.Print(rows + "," + cols + ": " + arrRecords.getItem(rows,cols));
            // close the RecordSet
            pRS.Close();
            pRS = null;
      }
      //close the DataSource
        pDS.Close();
      pDS = null;
}
//call OpenLayer with the layer Parks
// note there is a Parks layer in the sample Riverside.axf installed with ArcPad
Console.Clear();
OpenLayer("Parks");
©2012. All Rights Reserved.